package tij4.annotation.netCustom;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestTool {
    public static void main(String[] args) {
        NoBug noBug = new NoBug();
        Class<? extends NoBug> noBugClass = noBug.getClass();
        Method[] methods = noBugClass.getDeclaredMethods();
        //用来记录测试产生ｌｏｇ信息
        StringBuilder log = new StringBuilder();
        //记录异常的次数
        int errornum = 0;

        for (Method m : methods) {
            //只用被@jiecha标注过的方法才测试
            if (m.isAnnotationPresent(Jiecha.class)) {
                try {
                    m.setAccessible(true);
                    m.invoke(noBug, null);
                } catch (Exception e) {
//                    e.printStackTrace();
                    errornum++;
                    log.append(m.getName());
                    log.append(" ");
                    log.append("has error");
                    log.append("\n caused by ");
                    //记录测试过程中,发生的异常的名称
                    log.append(e.getCause().getClass().getSimpleName());
                    log.append("\n");
                    //记录测试过程中,发生的异常的具体信息
                    log.append(e.getCause().getMessage());
                    log.append("\n");
                }
            }
        }

        log.append(noBugClass.getSimpleName());
        log.append(" has ");
        log.append(errornum);
        log.append(" error.");

        //生成测试报告
        System.out.println(log);
    }
}
